// This file is downloaded from Moretti website, and edited by Matteo.
// original file name: table1_4.do
//link: https://eml.berkeley.edu/~moretti/data3.html



******************************
**  PRELIMINARY OPERATIONS
******************************

******************************
** Lagged values
******************************
use "data/table1/enricoall2.dta", clear
sort state district year
by state district year: g n = _n
keep if n<=2

replace year = year +2
g lagged = realada

	// Matteo adds controls
	recode sex (1=1) (2=0), g(male)
	tab lastoccup, g(doccup)
	tab collegeattend, g(dcoll)
	tab milit, g(dmil)
	tab second, g(dedu)
	global contr male relatives_shrt age ///
	doccup1 doccup2 doccup3 doccup4 doccup5 doccup6 doccup7 ///
	dcoll1 dcoll2 dcoll3 dcoll4 ///
	dmil1 dmil2 dmil3 ///
	dedu1 dedu2 dedu3
	foreach var in $contr {
	g lag_`var'=`var'
	}
keep state district year n lagged lag_*
sort state district year n
save tmp, replace

******************************
** Simultaneous values
******************************
use "data/table1/enricoall2.dta", clear
sort state district year
by state district year: g n = _n
keep if n<=2

drop lagada*
sort state district year n
merge state district year n using tmp
rm tmp.dta

* drop if last time was a redistricting year
drop if year ==1952 | year==1962 | year==1972 | year==1982 | year==1992
*keep if lagdemvoteshare>.48 & lagdemvoteshare<.52
drop democrat
g       democrat = 1 if demvoteshare>=.5
replace democrat = 0 if demvoteshare<.5

g       lagdemocrat = 1 if lagdemvoteshare >=.5
replace lagdemocrat = 0 if lagdemvoteshare <.5
g    score = realada
g lagscore = lagged
egen id = group(state district year n)
keep if score ~=. & lagscore ~=. | year==1946
keep if democrat~=. & lagdemocrat ~=. | year==1946
keep if id ~=.
replace lagdemocrat=. if year==1946

********************************
** Matteo adds Majority Status
********************************
g dem_maj = 1
replace dem_maj = 0 if congress==80 | congress==83
g majority = .
replace majority = democrat if dem_maj==1
replace majority = 1 - democrat if dem_maj==0
g lagdem_maj = 1
replace lagdem_maj = 0 if congress==81 | congress==84
replace lagdem_maj = . if congress==80
g lagmajority = .
replace lagmajority = lagdemocrat if lagdem_maj==1
replace lagmajority = 1 - lagdemocrat if lagdem_maj==0

********************************
** Matteo adds Time dummies
********************************
quietly: tab congress, g(dcon)

********************************
** Matteo adds Running variable
********************************
g dgap = demvoteshare-0.5
g lagdgap = lagdemvoteshare-0.5
g rdgap = dgap * democrat
g lagrdgap = lagdgap * lagdemocrat
g dgap2=dgap*dgap
g lagdgap2=lagdgap*lagdgap
g rdgap2=rdgap*rdgap
g lagrdgap2=lagrdgap*lagrdgap

********************************
** Matteo adds Contemporaneous Controls
********************************
recode sex (1=1) (2=0), g(male)
tab lastoccup, g(doccup)
tab collegeattend, g(dcoll)
tab milit, g(dmil)
tab second, g(dedu)

********************************
** Matteo rename vars shorter
********************************
g d = democrat
g lagd=lagdemocrat
g m = majority
g lagm = lagmajority
g dm = dem_maj
g lagdm = lagdem_maj
g inter_d_m = d*m


******************************
** END PRELIMINARY OPERATIONS
******************************


******************************
** REPLICATING TABLE 1
******************************

eststo clear

******************************
* 1) score lagdemocrat
******************************
quietly{
global y score
label variable $y "$RC_{t+1}$" 
global run lagdgap
global run_reg 
// lagdgap lagrdgap 
global lag_x  lag_male lag_relatives_shrt lag_age ///
	lag_doccup1 lag_doccup2 lag_doccup3 lag_doccup4 lag_doccup5 lag_doccup6 lag_doccup7 ///
	lag_dcoll1 lag_dcoll2 lag_dcoll3 lag_dcoll4 ///
	lag_dmil1 lag_dmil2 lag_dmil3 ///
	lag_dedu1 lag_dedu2 lag_dedu3 
	
	g bw=0
	replace bw`i'=1 if $run > -0.02 & $run <0.02
	 
		eststo clear
	 
		// to create sample: observations for which covariates (in $lag_x) are observed
		// this way the sample is the same with or without controls
		// (regressions with controls are relegated in Supplementary Material Table B3)
		reg $y lagd lagm $lag_x  $run_reg if bw==1, cluster(id)
		g s=1 if e(sample)
	 
		eststo: reg $y lagd $run_reg if bw==1 & s==1, cluster(id) 
		estadd local timefe "No" 
		estadd local control "No"
		
		eststo: reg $y lagd lagm $run_reg if bw==1 & s==1, cluster(id) 
		estadd local timefe "No" 
		estadd local control "No"
		
		eststo: reg $y lagd lagm dcon* $run_reg if bw==1 & s==1, cluster(id)
		estadd local timefe "Yes" 
		estadd local control "No"
		
		eststo: reg $y lagd $lag_x $run_reg if bw==1 & s==1, cluster(id)
		estadd local timefe "No" 
		quietly: test $lag_x
		estadd scalar pcontrol=r(p)
		estadd local control "Yes"
		
		eststo: reg $y lagd lagm $lag_x  $run_reg if bw==1 & s==1, cluster(id) 
		estadd local timefe "No" 
		quietly: test $lag_x
		estadd scalar pcontrol=r(p)
		estadd local control "Yes"
		
		eststo: reg $y lagd lagm $lag_x dcon* $run_reg if bw==1 & s==1, cluster(id)
		estadd local timefe "Yes" 
		quietly: test $lag_x
		estadd scalar pcontrol=r(p)
		estadd local control "Yes"
		
		
}	
	** View table (upper panel)
			#delimit ;
			esttab ,
				 se(2) b(2) nonotes 
				keep(lagd lagm )
				stats(
					timefe control pcontrol N ,
					labels(
						"Time-FE"
						"Controls"
						"Joint controls"
						"N"
						)
					fmt(%3s %3s 2 0 2)
				)				
				star(* 0.10 ** 0.05 *** 0.01)
				replace ;
			#delimit cr	
	

		** Latex
			#delimit ;
			esttab  using "results/tableB3_upper.tex",
				booktabs se(2) b(2) nonotes  nomtitles nostar
				keep(lagd lagm)
				stats(
					timefe control pcontrol N ,
					labels(
						"Time-FE"
						"Controls"
						"Joint controls"
						"N"
						)
					fmt(%3s %3s 2 0 2)
				)
				mgroups(
					" $ RC_{t+1}$ " 
					, pattern(1 0 0 ) 
					prefix(\multicolumn{@span}{c}{) 
					suffix(}) 
					span 
					erepeat(\cmidrule(lr){@span})
				)
				varlabels(
					lagd "D"
					lagm "M"
					)
				replace ;
			#delimit cr
	
eststo clear			

******************************
* 2) score democrat
******************************
quietly {
global y score
label variable $y "$RC_{t}$" 
global run dgap
global run_reg
// dgap rdgap 
global x  male relatives_shrt age ///
	doccup1 doccup2 doccup3 doccup4 doccup5 doccup6 doccup7 ///
	dcoll1 dcoll2 dcoll3 dcoll4 ///
	dmil1 dmil2 dmil3 ///
	dedu1 dedu2 dedu3

	drop bw
	g bw=0
	replace bw`i'=1 if $run > -0.02 & $run <0.02
		
	 
		// to create sample
		reg $y d m $x  $run_reg if bw==1, cluster(id)
		drop s
		g s=1 if e(sample)
	 
	 
		eststo: reg $y d $run_reg if bw==1 & s==1, cluster(id) 
		estadd local timefe "No" 
		estadd local control "No"
		
		eststo: reg $y d m $run_reg if bw==1 & s==1, cluster(id) 
		estadd local timefe "No" 
		estadd local control "No"
		
		eststo: reg $y d m dcon* $run_reg if bw==1 & s==1, cluster(id) 
		estadd local timefe "Yes" 
		estadd local control "No"
		
		eststo: reg $y d $x $run_reg $x if bw==1 & s==1, cluster(id) 
		estadd local timefe "No" 
		quietly: test $x
		estadd scalar pcontrol=r(p)
		estadd local control "Yes"
		
		eststo: reg $y d m $x $run_reg if bw==1 & s==1, cluster(id) 
		estadd local timefe "No" 
		quietly: test $x
		estadd scalar pcontrol=r(p)
		estadd local control "Yes"
		
		eststo: reg $y d m $x dcon* $run_reg if bw==1 & s==1, cluster(id)
		estadd local timefe "Yes" 	
		quietly: test $x
		estadd scalar pcontrol=r(p)
		estadd local control "Yes"		
}
		
	** View table (middle panel)
			#delimit ;
			esttab ,
				 se(2) b(2) nonotes 
				keep(d m )
				stats(
					timefe control pcontrol N ,
					labels(
						"Time-FE"
						"Controls"
						"Joint controls"
						"N"
						)
					fmt(%3s %3s 2 0 2)
				)
				star(* 0.10 ** 0.05 *** 0.01)
				replace ;
			#delimit cr	
	
	** Latex
			#delimit ;
			esttab  using "results/tableB3_middle.tex",
				booktabs se(2) b(2) nonotes  nomtitles nonumb nostar
				keep(d m)
				stats(
					timefe control pcontrol N ,
					labels(
						"Time-FE"
						"Controls"
						"Joint controls"
						"N"
						)
					fmt(%3s %3s 2 0 2)
				)
				mgroups(
					" $ RC_{t}$ " 
					, pattern(1 0 0 ) 
					prefix(\multicolumn{@span}{c}{) 
					suffix(}) 
					span 
					erepeat(\cmidrule(lr){@span})
				)
				varlabels(
					d "D"
					m "M"
					)
				replace ;
			#delimit cr	
			
eststo clear

******************************
* 3)  democrat lagdemocrat
******************************
quietly {
global y democrat
label variable $y "$D_{t+1}$" 
global run lagdgap
global run_reg lagdgap lagrdgap 
global lag_x  lag_male lag_relatives_shrt lag_age ///
	lag_doccup1 lag_doccup2 lag_doccup3 lag_doccup4 lag_doccup5 lag_doccup6 lag_doccup7 ///
	lag_dcoll1 lag_dcoll2 lag_dcoll3 lag_dcoll4 ///
	lag_dmil1 lag_dmil2 lag_dmil3 ///
	lag_dedu1 lag_dedu2 lag_dedu3
	
	
	drop bw
	g bw=0
	replace bw`i'=1 if $run > -0.02 & $run <0.02
	 
		
		eststo clear
	 
		// to create sample
		reg $y lagd lagm $lag_x  $run_reg if bw==1, cluster(id)
		drop s
		g s=1 if e(sample)
	 
		eststo: reg $y lagd $run_reg if bw==1 & s==1, cluster(id) 
		estadd local timefe "No" 
		estadd local control "No"
		
		eststo: reg $y lagd lagm $run_reg if bw==1 & s==1, cluster(id) 
		estadd local timefe "No" 
		estadd local control "No"
		
		eststo: reg $y lagd lagm dcon* $run_reg if bw==1 & s==1, cluster(id) 
		estadd local timefe "Yes" 
		estadd local control "No"
		
		eststo: reg $y lagd $lag_x $run_reg if bw==1 & s==1, cluster(id) 
		estadd local timefe "No" 
		quietly: test $lag_x
		estadd scalar pcontrol=r(p)
		estadd local control "Yes"
		
		eststo: reg $y lagd lagm $lag_x  $run_reg if bw==1 & s==1, cluster(id)
		estadd local timefe "No" 
		quietly: test $lag_x
		estadd scalar pcontrol=r(p)
		estadd local control "Yes"
		
		eststo: reg $y lagd lagm $lag_x dcon* $run_reg if bw==1 & s==1, cluster(id) 
		estadd local timefe "Yes" 
		quietly: test $lag_x
		estadd scalar pcontrol=r(p)
		estadd local control "Yes"
}	
		
	** View table columns: (lower panel)
			#delimit ;
			esttab ,
				 se(3) b(3) nonotes 
				keep(lagd lagm )
				stats(
					timefe control pcontrol N ,
					labels(
						"Time-FE"
						"Controls"
						"Joint controls"
						"N"
						)
					fmt(%3s %3s 2 0 2)
				)
				star(* 0.10 ** 0.05 *** 0.01)
				replace ;
			#delimit cr	
	
	

	** Latex
			#delimit ;
			esttab  using "results/tableB3_lower.tex",
				booktabs se(3) b(3) nonotes  nomtitles nonumb nostar
				keep(lagd lagm)
				stats(
					timefe control pcontrol N ,
					labels(
						"Time-FE"
						"Controls"
						"Joint controls"
						"N"
						)
					fmt(%3s %3s 2 0 2)
				)
				mgroups(
					" $ D_{t+1}$ " 
					, pattern(1 0 0 ) 
					prefix(\multicolumn{@span}{c}{) 
					suffix(}) 
					span 
					erepeat(\cmidrule(lr){@span})
				)
				varlabels(
					lagd "D"
					lagm "M"
					)
				replace ;
			#delimit cr
